package wetalk.software.bupt.com.wetalk.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.promeg.pinyinhelper.Pinyin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import wetalk.software.bupt.com.wetalk.R;
import wetalk.software.bupt.com.wetalk.model.po.User;
import wetalk.software.bupt.com.wetalk.util.MyPinyinParser;
import wetalk.software.bupt.com.wetalk.util.PinYinUtil;
import wetalk.software.bupt.com.wetalk.util.PinyinComparator;
import wetalk.software.bupt.com.wetalk.util.ViewHolder;
import wetalk.software.bupt.com.wetalk.view.viewinter.MyLetterView;


/**
 * Created by Administrator on 2017/11/30.
 */

public class ContactAdapter extends BaseAdapter  {

    public static final String TAG="ContactAdapter";
    private Context mContext;
    private List<User> userList;// 好友信息
    MyPinyinParser mParser;

    public ContactAdapter(List<User> list){
        userList=list;
    }

    public ContactAdapter(Context mContext, List<User> list) {
        if(list==null){
            this.userList=new ArrayList<>();
        }else{
            this.userList = list;
        }
        this.mContext = mContext;
        mParser=new MyPinyinParser();
    }

    @Override
    public int getCount() {
        return userList.size();
    }

    @Override
    public Object getItem(int position) {
        return userList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(userList.size()!=0){
            User user = userList.get(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.contact_item, null);
            }
            ImageView ivAvatar = ViewHolder.get(convertView,
                    R.id.contactitem_avatar_iv);
            TextView tvCatalog = ViewHolder.get(convertView,
                    R.id.contactitem_catalog);
            TextView tvNick = ViewHolder.get(convertView, R.id.contactitem_nick);
            String catalog = mParser.toComplexPy(user.getUserName())
                    .substring(0, 1);
            if (position == 0) {
                tvCatalog.setVisibility(View.VISIBLE);
                tvCatalog.setText(catalog);
            } else {
                User Nextuser = userList.get(position - 1);
                String lastCatalog = mParser.toComplexPy(
                        Nextuser.getUserName()).substring(0, 1);
                if(catalog.charAt(0)<65 || (catalog.charAt(0)>90 && catalog.charAt(0)<97) || catalog.charAt(0)>122){
                    if(lastCatalog.charAt(0)<65 || (lastCatalog.charAt(0)>90 && lastCatalog.charAt(0)<97) || lastCatalog.charAt(0)>122){
                        tvCatalog.setVisibility(View.GONE);
                    } else {
                        tvCatalog.setVisibility(View.VISIBLE);
                        tvCatalog.setText("#");
                    }
                } else{
                    if (catalog.toLowerCase().equals(lastCatalog.toLowerCase())) {
                        tvCatalog.setVisibility(View.GONE);
                    } else {
                        tvCatalog.setVisibility(View.VISIBLE);
                        tvCatalog.setText(catalog.toUpperCase());
                    }
                }

            }

            ivAvatar.setImageResource(R.drawable.head);
            tvNick.setText(user.getUserName());
            return convertView;
        }else{
            return null;
        }

    }

    public void updateListView(List<User> list){
        userList=list;
        notifyDataSetChanged();
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public int getPositionForSelection(char s){
        MyPinyinParser myPinyinParser=new MyPinyinParser();
        for (int i = 0; i <userList.size() ; i++) {
            char c= myPinyinParser.toComplexPy(userList.get(i).getUserName()).toUpperCase().charAt(0);

            if (s==c){
                return i+1;
            }else if(s<65 || (s>90 && s<97) || s>122){
                    if(c<65 || (c>90 && c<97) || c>122){
                        return i+1;
                }
            }
        }
       return -1;
    }

}
